﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace SMS
{
    public partial class uDanhsachkhachhang : UserControl
    {

        fLogin main;
        public uDanhsachkhachhang(fLogin main1)
        {
            InitializeComponent();
            main = main1;
        }


        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        SqlDataAdapter adapter;
        SqlConnection conn;
        SqlCommand command;
        SqlCommand command2;
  
        

        public void uDanhsachkhachhang_Load(object sender, EventArgs e)
        {
            DataTable dtbResult = new DataTable();

            conn = new SqlConnection(main.ConnectDB);
            command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT HOTEN, KH.CMND , COUNT(XE.SOKHUNG) AS '" + " SO XE DA MUA" + "' FROM KHACHHANG KH, XEMAY XE" +
                                    " WHERE XE.CMND = KH.CMND and tinhtrang='" + "yes" + "'" +
                                    "  GROUP BY HOTEN, KH.CMND";
            adapter = new SqlDataAdapter(command);
            try
            {
                adapter.Fill(dtbResult);

            }
            catch (Exception ex)
            {
                string err = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }
            dgvDSKH.DataSource = dtbResult;
            
        }

        private void btnImportDanhSachKhachHang_Click(object sender, EventArgs e)
        {
            main.TraCuuSinhNhatKhachHang.ShowDialog();
            
        }

 
        public void loaddatagridview(DataGridView dg, string strselect)
        {
            conn = new SqlConnection(main.ConnectDB);
            ds.Clear();
            adapter = new SqlDataAdapter(strselect, main.ConnectDB);
            adapter.Fill(ds, "KHACHHANG");
            dg.DataSource = ds.Tables[0];
        }

        public void loadxe()
        {

            //--------------------------------
            int i = 0;
            string[] DSSoKhung = new string[1000];
            int selectRow = dgvDSKH.CurrentRow.Index;
            command2 = new SqlCommand();
            command2.Connection = conn;
            command2.CommandText = "select sokhung from xemay where CMND= '" + dgvDSKH.Rows[selectRow].Cells[1].Value.ToString() + "'";
            //MessageBox.Show("CMND" + dgvDSKH.Rows[selectRow].Cells[1].Value.ToString());
            try
            {
                conn.Open();
                SqlDataReader reader = command2.ExecuteReader();
                while (reader.Read())
                {
                    DSSoKhung[i] = reader[0].ToString();
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    main.Thongtinkhachhang.lstXeDaMua.Items.Add(DSSoKhung[j].ToString());
                }
            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command2.Dispose();
                conn.Close();
            }

            //------------------------------


        }
        public void loadkh()
        {

            int selectRow = dgvDSKH.CurrentRow.Index;
            conn = new SqlConnection(main.ConnectDB);
            command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "select * from KHACHHANG where CMND = '" + dgvDSKH.Rows[selectRow].Cells[1].Value.ToString()+ "'" ;


            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {

                    main.Thongtinkhachhang.txtTenKH.Text = (reader["HOTEN"].ToString());
                    main.Thongtinkhachhang.txtBidanh.Text = (reader["BIDANH"].ToString());
                    main.Thongtinkhachhang.cboGT.Text = (reader["GIOITINH"].ToString());
                    main.Thongtinkhachhang.txtDiaChi.Text = (reader["DIACHI"].ToString());
                    main.Thongtinkhachhang.txtQuanHuyen.Text = (reader["QUANHUYEN"].ToString());
                    main.Thongtinkhachhang.txtThanhPho.Text = (reader["TINHTP"].ToString());
                    main.Thongtinkhachhang.txtCMND.Text = (reader["CMND"].ToString());
                    //main.Thongtinkhachhang.txtTinhtrang.Text = (reader["TINHTRANG"].ToString());
                    main.Thongtinkhachhang.txtNN.Text = (reader["NGHENHIEP"].ToString());
                    main.Thongtinkhachhang.cboNgay.Text = (reader["NGAYSINH"].ToString());
                    main.Thongtinkhachhang.txtNam.Text = (reader["NAMSINH"].ToString());
                    main.Thongtinkhachhang.cboThang.Text = (reader["THANGSINH"].ToString());
                    main.Thongtinkhachhang.txtDTDiDong.Text = (reader["DTDIDONG"].ToString());
                    main.Thongtinkhachhang.txtSodtnha.Text = (reader["DTNHA"].ToString());



                }


            }
            catch (SqlException ex)
            {
                string err = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
        public void dgvDSKH_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {


            loadkh();
            loadxe();

            main.Thongtinkhachhang.ShowDialog();


        }

        private void lblDanhSachKhachHang_Click(object sender, EventArgs e)
        {

        }

        private void dgvDSKH_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
